package cn.jinjimi.app.main.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

/**
 * Created by lforxeverc on 16/8/16.
 */
public class TagDrawable extends Drawable {
    public final static int TypeRect = 0;
    public final static int TypeCircle = 1;
    public final static int TypeHalfCircle = 2;
    private int type;

    Paint mPaint;
    String text;
    int width;
    int height;
    int paddingH = 5;
    int paddingV;
    int marginLeft;
    int marginBottom;
    float density;
    int baseline;

    int textColor;
    int strokeColor;
    float radius;

    public TagDrawable(Context mContext, float textSize, String text, int height) {
        this.height = height;
        this.text = text;

        mPaint = new Paint();

        density = mContext.getResources().getDisplayMetrics().density;
        paddingH = (int) (paddingH * density);

        mPaint.setAntiAlias(true);
        mPaint.setTextAlign(Paint.Align.CENTER);

        mPaint.setTextSize(textSize);

        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        baseline = (int) ((height - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top);

        Rect bounds = new Rect();
        mPaint.getTextBounds(text, 0, text.length(), bounds);
        width = bounds.right - bounds.left + paddingH * 2 + marginLeft * 2;
        setBounds(0, 0, width, height);
    }

    public TagDrawable TextColor(int color) {
        this.textColor = color;
        return this;
    }

    public TagDrawable StrokeColor(int color) {
        this.strokeColor = color;
        return this;
    }

    public TagDrawable Radius(float radius) {
        this.radius = radius;
        return this;
    }

    public TagDrawable paddingH(int paddingH) {
        Rect bounds = new Rect();
        mPaint.getTextBounds(text, 0, text.length(), bounds);
        width = bounds.right - bounds.left + paddingH * 2 + marginLeft * 2;
        setBounds(0, 0, width, height);
        return this;
    }

    public TagDrawable type(int type) {
        this.type = type;
        return this;
    }

    public TagDrawable marginLeft(int marginLeft) {
        this.marginLeft = marginLeft;
        Rect bounds = new Rect();
        mPaint.getTextBounds(text, 0, text.length(), bounds);
        width = bounds.right - bounds.left + paddingH * 2 + marginLeft * 2;
        setBounds(0, 0, width, height);
        return this;
    }

    public TagDrawable marginBottom(int marginBottom) {
        this.marginBottom = marginBottom;
        Rect bounds = new Rect();
        mPaint.getTextBounds(text, 0, text.length(), bounds);
        height = height + marginBottom;
        setBounds(0, 0, width, height);
        return this;
    }

    @Override
    public void draw(Canvas canvas) {

        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(textColor);
        canvas.drawText(text, (width) / 2, baseline, mPaint);
        mPaint.setStrokeWidth(1);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(strokeColor);

        if (type == TypeRect) {
            RectF rect = new RectF(1 + marginLeft, 0, width - 1 - marginLeft, height - marginBottom);
            canvas.drawRoundRect(rect, radius, radius, mPaint);
        } else if (type == TypeHalfCircle) {
            float tx = height / 2 - 3;
            float ty = 3;
            float bx = width - (height - 2 * 3) / 2;
            float by = height - 3;
            Path line1 = new Path();
            line1.moveTo(tx, ty);
            line1.lineTo(bx, ty);
            canvas.drawPath(line1, mPaint);
            Path line2 = new Path();
            line2.moveTo(tx, by);
            line2.lineTo(bx, by);
            canvas.drawPath(line2, mPaint);
            RectF rectf = new RectF(1, 3, height - 2 * 3, height - 3);
            canvas.drawArc(rectf, 90, 180, false, mPaint);
            RectF rectR = new RectF(width - (height - 2 * 3), 3, width - 1, height - 3);
            canvas.drawArc(rectR, -90, 180, false, mPaint);
        }

    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        mPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public int getIntrinsicHeight() {
        return height;
    }

    @Override
    public int getIntrinsicWidth() {
        return width;
    }
}